<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Constants\ErrorCode;
use App\Constants\HttpStatus;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;

/**
 * @Controller()
 * */
class UserController extends AbstractController
{
    /**
     * @RequestMapping(path="index", methods="get")
     * */
    public function index(RequestInterface $request)
    {
        $user   = $request->input('user', 'Hyperf');
        $method = $request->getMethod();
        $user_info   = Db::table('user')->where('id', '=', 1)->first();

        return [
            'method' => $method,
            'message' => "Hello {$user}.12345678",
            'user'      => $user_info
        ];
    }

    /**
     * @RequestMapping(path="index", methods="post")
     * */
    public function create(RequestInterface $request)
    {
        return [
            'method' => 'post',
            'message' => "这是POST方法",
        ];
    }

    /**
     * @RequestMapping(path="index", methods="put")
     * */
    public function update(RequestInterface $request)
    {
        $container = ApplicationContext::getContainer();
        $redis  = $container->get(Redis::class);
        $result = $redis->keys('*');
        var_dump($result);
        return [
            'method' => 'put',
            'message' => "这是PUT方法",
            'status'    => HttpStatus::getMessage(HttpStatus::HTTP_NO_CONTENT),
            'data'      => $result
        ];
    }

    /**
     *
     * @DeleteMapping(path="index")
     * */
    public function del(RequestInterface $request)
    {
        return [
            'method' => 'delete',
            'message' => "这是DELETE方法",
        ];
    }
}
